So all I want this program to do is if i typed in
I know i dont even need the cat, i'm just trying to learn the pipe part of this also.
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
void sighandler(int id)
{
printf("Got a signal\n");
}
int main(int argc, char *argv[])
{
/* do a ls | cat */
char *ls = "ls";
char *cat = "cat";
char *lsargs[] = {"ls"};
char *catargs[] = {"cat"};
struct sigaction asignal;
sigemptyset(&asignal.sa_mask);
asignal.sa_handler = sighandler;
asignal.sa_flags = 0;
sigaction(SIGCHLD, &asignal, (struct sigaction *) NULL);
int status;
int pid = fork();
if(pid > 0)
{
/* parent waits for SIGCHLD */
waitpid(-1, &status, 0);
printf("children finished\n");
}
if(pid == 0)
{
/* this should spawn an ls. redirect output to cat */
int fd[2];
pipe(fd);
int cid = fork();
if(cid > 0)
{
/* this is the ls */
close(fd[0]); /* close read pipe */
close(STDOUT_FILENO); /* close standard out */
dup(fd[1]); /* make write pipe stand ard out */
close(fd[1]); /* close my ptr to write pipe */
execve(ls, lsargs, 0);
exit(0);
}
else
{
/* this is the cat */
close(fd[1]); /* close write pipe */
close(STDIN_FILENO); /* close standard in */
dup(fd[0]); /* make read pipe standard in */
close(fd[0]); /* close my ptr to read pipe */
execve(cat, catargs, 0);
exit(0);
}
}
printf("completed.\n");
}
When i run this i'm assuming it works fine in the background, but i get no output except for the printfs() in the parent process. Is there something im doing wrong here that is preventing me from seeing output? All open FD's get closed so nothing is hanging.
Parent process stdin and stdout are untouched.
Child 1 is made.
Child 2 is made.
---
After this child 1 and child2's stdout and stdin are messed with for piping. So child 2 should be printing to standard out.